<html>
<head><meta charset="utf-8"><title>zst read/write from/to dangling pointer · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html">zst read/write from/to dangling pointer</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="184096917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184096917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184096917">(Dec 23 2019 at 10:54)</a>:</h4>
<blockquote>
<p>I thought writes of zsts to dangling pointers is fine?</p>
</blockquote>
<p>Not always, no. If the pointer is a <code>Pointer</code> (not an integer), but dangling (out-of-bounds or use-after-free), it's UB even with ZST. Also see <a href="https://github.com/rust-lang/miri/blob/a3ea1cb4581970396635fce82182555003083b62/tests/compile-fail/dangling_zst_deref.rs" target="_blank" title="https://github.com/rust-lang/miri/blob/a3ea1cb4581970396635fce82182555003083b62/tests/compile-fail/dangling_zst_deref.rs">this test</a>.</p>
<p>That seems very fragile. I can totally see wg-mir-opt creating an optimization pass that just removes all reads and writes to/from zsts from the MIR</p>



<a name="184096951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184096951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184096951">(Dec 23 2019 at 10:55)</a>:</h4>
<p>then that test would suddenly run successfully</p>



<a name="184096972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184096972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184096972">(Dec 23 2019 at 10:55)</a>:</h4>
<p>Ok, technically we'd still need an alignment check left over</p>



<a name="184097033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184097033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184097033">(Dec 23 2019 at 10:56)</a>:</h4>
<p>hmm ok, if this is UB, then we're allowed to optimize it</p>



<a name="184097042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184097042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184097042">(Dec 23 2019 at 10:56)</a>:</h4>
<p>so the optimization would still be sound</p>



<a name="184098911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184098911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184098911">(Dec 23 2019 at 11:37)</a>:</h4>
<blockquote>
<p>That seems very fragile. I can totally see wg-mir-opt creating an optimization pass that just removes all reads and writes to/from zsts from the MIR</p>
</blockquote>
<p>it's okay for opzimizations to remove UB</p>



<a name="184098917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184098917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184098917">(Dec 23 2019 at 11:37)</a>:</h4>
<p>what you said</p>



<a name="184098965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/zst%20read/write%20from/to%20dangling%20pointer/near/184098965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/zst.20read.2Fwrite.20from.2Fto.20dangling.20pointer.html#184098965">(Dec 23 2019 at 11:38)</a>:</h4>
<p>so, yeah, reliable UB-testing requires disabling all optimizations. no surprise there.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>